package com.aspsine.horizontalautoscrollview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

/**
 * Created by aspsine on 15/12/2.
 */
public class HorizontalAutoScrollView extends RecyclerView {

    private static final boolean DEBUG = false;

    private static final int MAX = Integer.MAX_VALUE;

    private static final int STEP = 2;

    private static final int DELAY_TIME = 60;

    private boolean mRunning;

    public HorizontalAutoScrollView(Context context) {
        this(context, null);
    }

    public HorizontalAutoScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalAutoScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                stop();
                break;
            case MotionEvent.ACTION_MOVE:
                stop();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                start();
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    public void init(String imageUri, int width, int height) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        setLayoutManager(layoutManager);
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(width, height, imageUri);
        setAdapter(adapter);
        scrollToPosition(MAX / 2);
    }

    public boolean isRunning() {
        return mRunning;
    }

    public void start() {
        if (!mRunning) {
            post(mRunnable);
            mRunning = true;
        }
    }

    public void stop() {
        if (mRunning) {
            removeCallbacks(mRunnable);
            mRunning = false;
        }
    }

    private final Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            scroll();
        }
    };

    private void scroll() {
        scrollBy(STEP, 0);
        postDelayed(mRunnable, DELAY_TIME);
    }

    private static class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private final String mImageUri;

        private final int mWidth;

        private final int mHeight;

        private final DisplayImageOptions mOption;

        public RecyclerViewAdapter(int width, int height, String imageUri) {
            this.mWidth = width;
            this.mHeight = height;
            this.mImageUri = imageUri;

            mOption = new DisplayImageOptions.Builder()
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .resetViewBeforeLoading(false)
                    .build();
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            final ImageView imageView = new ImageView(parent.getContext());
            final RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(mWidth, mHeight);
            imageView.setLayoutParams(lp);

            return new ViewHolder(imageView);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            final ImageView imageView = (ImageView) holder.itemView;
            if (DEBUG) {
                if (position % 2 == 0) {
                    imageView.setBackgroundColor(Color.BLUE);
                } else {
                    imageView.setBackgroundColor(Color.WHITE);
                }
            }
            ImageLoader.getInstance().displayImage(mImageUri, imageView, mOption, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    int width = loadedImage.getWidth();
                    RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) imageView.getLayoutParams();
                    lp.width = width;
                    imageView.requestLayout();
                }
            });

        }

        @Override
        public int getItemCount() {
            return MAX;
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}
